package com.zhijian.wharf.controller.web;

import com.zhijian.response.annotation.Response;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.annotation.LogOperation;
import com.zhijian.wharf.entity.po.User;
import com.zhijian.wharf.entity.vo.request.UserSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.request.UserSearchVo;
import com.zhijian.wharf.entity.vo.response.UserVo;
import com.zhijian.wharf.holder.CurrentWebUserHolder;
import com.zhijian.wharf.job.MysqlDeleteJob;
import com.zhijian.wharf.job.MysqlDumpJob;
import com.zhijian.wharf.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人员 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-01 15:41:25
 */
@Slf4j
@Controller
@Api(tags = "人员")
public class UserController {

    @Autowired
    private UserService service;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/user")
    @ApiOperation("新增")
    @LogOperation("新增账户")
    public boolean insert(@RequestBody UserSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != 0;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/user/{id}")
    @ApiOperation("删除")
    @LogOperation("删除账户")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/user/{id}")
    @ApiOperation("修改")
    @LogOperation("修改账户")
    public boolean update(@PathVariable("id") Long id, @RequestBody UserSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    @Autowired
    private MysqlDeleteJob mysqlDeleteJob;

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/user/{id}")
    @ApiOperation("根据id查询")
    @LogOperation("查询账户详情")
    public UserVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/user/page")
    @ApiOperation("分页查询")
    @LogOperation("分页查询账户")
    public Page<UserVo> selectByCondition(@RequestBody(required = false) UserSearchVo param) {
        if (param == null)
            param = UserSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/user/list")
    @ApiOperation("列表查询，不分页")
    @LogOperation("列表查询账户")
    public List<UserVo> selectList(@RequestBody(required = false) UserSearchVo param) {
        if (param == null)
            param = UserSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectList(param);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @Response
    @PostMapping("/user/status/{id}/{status}")
    @ApiOperation("修改状态")
    public boolean changeStatus(@ApiParam(name = "id", value = "权限ID", required = true)
                                @PathVariable("id") Long id,
                                @ApiParam(name = "status", value = "开关值，0：开启，1：关闭", required = true)
                                @PathVariable("status") Byte status) {
        return service.updateByPrimaryKeySelective(User.builder()
                .id(id)
                .status(status)
                .build());
    }

    /**
     * 登录用户的信息
     *
     * @return
     */
    @Response
    @GetMapping("/user/me")
    @ApiOperation("登录用户的信息")
    public UserVo myInfo() {
        return service.selectVoById(CurrentWebUserHolder.getUser().getId());
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    @Response
    @DeleteMapping("/user/list")
    @ApiOperation("批量删除")
    public boolean delete(@RequestBody(required = false) List<Long> idList) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), idList);
        service.deleteByIdList(idList);
        return true;
    }

}